package cn.gogpay.dcb.ixc.cloud.common.dto;

import java.io.Serializable;
import java.util.List;

/**
 * 分页数据返回
 *
 * @author YinHeng
 * @date 2021/5/24 17:05
 */
public class PageResult<T> implements Serializable {

    private List<T> content;

    private Integer totalElements;

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    /**
     * 根据分页参数自动截取分页数据
     *
     * @param list
     * @param pageIndex
     * @param pageSize
     */
    public void setContent(List<T> list, int pageIndex, int pageSize) {
        this.totalElements = list.size();
        if (list.size() == 0) {
            this.content = list;
            return;
        }
        int fromIndex = Math.min((pageIndex - 1) * pageSize, list.size() - 1);
        int toIndex = Math.min((fromIndex + pageSize), list.size());
        this.content = list.subList(fromIndex, toIndex);
    }

    public Integer getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(Integer totalElements) {
        this.totalElements = totalElements;
    }

}
